<?php
/**
 * Sociable Bookmarks for Post Page
 */
function webtreats_save_short_url($id,$format = 'txt',$history = 1) {
	/* Based on code from David Walsh – http://davidwalsh.name/bitly-php */
	
	global $enable_bitly, $bitly_login, $bitly_api;
	
	$url = get_post_meta($id, 'bitly_trim', true);
	
	$post_url = get_permalink($id);
	
	if(!$enable_bitly){
		
		if($url){ return $url; }
		
		$url = sprintf('%s?p=%s', get_bloginfo('url').'/', $id);
		add_post_meta($id, 'bitly_trim', $url);
		return $url;
	}
	
	if( ($enable_bitly) && ($bitly_login) && ($bitly_api) ){
		
		if ( !stristr( $url, 'bit.ly' )) {
			
			$bitly = 'http://api.bit.ly/v3/shorten?login='.$bitly_login.'&apiKey='.$bitly_api.'&uri='.urlencode($post_url).
			'&format='.$format.'&history='.$history;
	
			$response = wp_remote_retrieve_body(wp_remote_get($bitly));
	
			if(is_wp_error($response)){
				$url = sprintf('%s?p=%s', get_bloginfo('url').'/', $id);
				add_post_meta($id, 'bitly_trim', $url);
				return $url;
			}
	
			$url = trim($response);
		
			delete_post_meta($id, 'bitly_trim');
			add_post_meta($id, 'bitly_trim', $url);
			
			return $url;
		}
		
		return $url;
		
	}
	
	if($url){ return $url; }
	
	$url = sprintf('%s?p=%s', get_bloginfo('url').'/', $id);
	add_post_meta($id, 'bitly_trim', $url);
	return $url;
}

function webtreats_sociable_bookmarks() {
	global $wp_query, $post;
	
	$sociable_sites = array (

		array( "name" => "Twitter",
			'icon' => get_template_directory_uri() . '/styles/_shared/post_sociables_twitter.png',
			'class' => 'twitter_icon',
			'url' => 'http://twitter.com/home?status=TITLE%20-%20PERMALINK',
		),

	    array( "name" => "StumbleUpon",
		    'icon' => get_template_directory_uri() . '/styles/_shared/post_sociables_stumbleupon.png',
			'class' => 'stumbleupon_icon',
		    'url' => 'http://www.stumbleupon.com/submit?url=PERMALINK&amp;title=TITLE',
		),

		array( "name" => "Reddit",
			'icon' => get_template_directory_uri() . '/styles/_shared/post_sociables_reddit.png',
			'class' => 'reddit_icon',
			'url' => 'http://reddit.com/submit?url=PERMALINK&amp;title=TITLE',
		),

		array( "name" => "Digg",
			'icon' => get_template_directory_uri() . '/styles/_shared/post_sociables_digg.png',
			'class' => 'digg_icon',
			'url' => 'http://digg.com/submit?phase=2&amp;url=PERMALINK&amp;title=TITLE&amp;bodytext=EXCERPT',
		),

		array( "name" => "del.icio.us",
			'icon' => get_template_directory_uri() . '/styles/_shared/post_sociables_delicious.png',
			'class' => 'delicious_icon',
			'url' => 'http://delicious.com/post?url=PERMALINK&amp;title=TITLE&amp;notes=EXCERPT',
		),
		
		array( "name" => "Facebook",
			'icon' => get_template_directory_uri() . '/styles/_shared/post_sociables_facebook.png',
			'class' => 'facebook_icon',
			'url' => 'http://www.facebook.com/share.php?u=PERMALINK&amp;t=TITLE',
		),
		
		array( "name" => "LinkedIn",
			'icon' => get_template_directory_uri() . '/styles/_shared/post_sociables_linkedin.png',
			'class' => 'linkedin_icon',
			'url' => 'http://www.linkedin.com/shareArticle?mini=true&amp;url=PERMALINK&amp;title=TITLE&amp;source=BLOGNAME&amp;summary=EXCERPT',
		),

	);
	
	// Load the post's and blog's data
	$blogname = urlencode(get_bloginfo('name')." ".get_bloginfo('description'));
	$post = $wp_query->post;
	
	
	// Grab the excerpt, if there is no excerpt, create one
	$excerpt = urlencode(strip_tags(strip_shortcodes($post->post_excerpt)));
	if ($excerpt == "") {
		$excerpt = urlencode(substr(strip_tags(strip_shortcodes($post->post_content)),0,250));
	}
	
	// Clean the excerpt for use with links
	$excerpt = str_replace('+','%20',$excerpt);
	$excerpt = str_replace('%0D%0A','',$excerpt);
	
	$permalink = webtreats_save_short_url($post->ID);
	
	$title = str_replace('+','%20',urlencode($post->post_title));
	
	foreach($sociable_sites as $bookmark) {	
		$url = $bookmark['url'];
		$url = str_replace('TITLE', $title, $url);
		$url = str_replace('BLOGNAME', $blogname, $url);
		$url = str_replace('EXCERPT', $excerpt, $url);
		$url = str_replace('PERMALINK', $permalink, $url);
		
		$output .= '<a href="' .$url. '">';
		$output .= '<img alt="' .$bookmark['name'].'" class="sociable_hover" src="' .$bookmark['icon'].'" />';
		$output .= '</a>';
	}

	return $output;
}

?>